package com.winchain.chainbill.asset.biz.query.service.impl;

import com.shell.bee.acct.transaction.vo.AcctInfo;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.constant.rel.AssetCode;
import com.winchain.chainbill.asset.biz.query.dao.AssetsQueryDao;
import com.winchain.chainbill.asset.biz.query.service.AssetsQueryService;
import com.winchain.chainbill.asset.biz.query.vo.Asset;
import com.winchain.chainbill.asset.biz.query.vo.Assets;
import com.winchain.chainbill.asset.biz.query.vo.CreditAssetVo;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("assetsQueryService")
public class AssetsQueryServiceImpl implements AssetsQueryService {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private AssetsQueryDao assetsQueryDao;

	public Assets query(String cusId) {
		BigDecimal amount = new BigDecimal(0);

		Asset cr = getAsset(cusId, "1", AssetCode.XQT.name());

		amount = amount.add(cr.getAmount());

		Assets as = new Assets();
		as.setAmount(amount);
		as.addAsset(cr);

		return as;
	}

	private Asset getAsset(String cusId, String acctType, String currCode) {
		BigDecimal amount = new BigDecimal(0);
		BigDecimal balance = new BigDecimal(0);
		BigDecimal used = new BigDecimal(0);
		BigDecimal balancePercent = new BigDecimal(0);
		String chainAddr = "";

		AcctInfo acctInfo = new AcctInfo();
		acctInfo.setCustId(cusId);
		acctInfo.setAcctType(acctType);
		acctInfo.setCurrCode(currCode);
		List<AcctInfo> AcctInfos = this.billAcctService.getAcctInfos(acctInfo);
		if ((AcctInfos != null) && (AcctInfos.size() > 0)) {
			AcctInfo fAcctInfo = (AcctInfo) AcctInfos.get(0);
			amount = fAcctInfo.getAcctAmt();
			balance = fAcctInfo.getAcctBlc();
			used = amount.subtract(balance);
			chainAddr = fAcctInfo.getAcctNo();
		}
		if (amount.compareTo(BigDecimal.ZERO) == 0) {
			balancePercent = BigDecimal.ZERO;
		} else {
			balancePercent = balance.divide(amount, 2, 4).multiply(new BigDecimal(100));
		}
		Asset asset = new Asset();
		asset.setAmount(amount);
		asset.setBalance(balance);
		asset.setUsed(used);
		asset.setType(acctType);
		asset.setBalancePercent(balancePercent);
		asset.setChainAddr(chainAddr);
		return asset;
	}

	@Pagination
	public List<CreditAssetVo> selectCreditAsset(PageLimit limit, CreditAssetVo creditAssetVo) {
		creditAssetVo.setBizCode("KDYW");
		if (AssetCode.XQT.name().equals(creditAssetVo.getCurrCode())) {
			creditAssetVo.setBizType("40");
		} else if (AssetCode.XTB.name().equals(creditAssetVo.getCurrCode())) {
			creditAssetVo.setBizType("41");
		} else if (AssetCode.FQT.name().equals(creditAssetVo.getCurrCode())) {
			creditAssetVo.setBizType("42");
		}
		return this.assetsQueryDao.selectCreditAsset(creditAssetVo);
	}
}